package com.tarena.lbs.basic.web.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.tarena.lbs.basic.web.repository.AreaRepository;
import com.tarena.lbs.pojo.basic.po.AreaPO;
import com.tarena.lbs.pojo.basic.vo.AreaVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AreaService {
    @Autowired
    private AreaRepository areaRepository;

    public List<AreaVO> queryAreasByParentId(Integer parentId) {
        //1.redis的缓存key值 前缀+业务+后缀 areas:{parentId}
        //2.redis去读
        //3. 判断命中
        //4. 命中直接返回list
        //5. 没命中查数据库 存储redis
        //从仓储层拿pos 非空判断 转成vo
        List<AreaVO> vos=null;
        List<AreaPO> pos=areaRepository.getAreasByParentId(parentId);
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                AreaVO vo=new AreaVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }
    //转化 和封装 常见的方法名称 mapperVOPO mapperPOVO
    //vo2po po2vo vos2pos pos2vos assemble**
    public List<AreaVO> areaTree() {
        //1.查询所有 地市数据
        List<AreaPO> pos=areaRepository.queryAll();
        //2.将所有数据装配成tree
        return assembleAreeTree(pos);
    }

    private List<AreaVO> assembleAreeTree(List<AreaPO> pos) {
        //1.判断空
        if (CollectionUtils.isEmpty(pos)){
            return null;
        }
        //2.树结构 从省级列表 到市级列表 到区级 将省从pos筛选出来 parentId=0
        List<AreaPO> provincePos=pos.stream().filter(po->po.getParentId()==0).collect(Collectors.toList());
        //2.1将每一个级别的pos列表 先转成vos 循环vos
        List<AreaVO> provinceVos=pos2vos(provincePos);
        log.info("省级单位list:{}",provinceVos);
        //3.循环省级pos 转化成vo,从总pos拿到该省下面的所有市
        for (AreaVO provinceVo : provinceVos) {
            List<AreaPO> cityPos=pos.stream().filter(po->po.getParentId().equals(provinceVo.getId())).collect(Collectors.toList());
            List<AreaVO> cityVos=pos2vos(cityPos);
            log.info("当前省:{},下一级城市list:{}",provinceVo.getName(),cityVos);
            provinceVo.setChildren(cityVos);
            for (AreaVO cityVo : cityVos) {
                List<AreaPO> areaPos=pos.stream().filter(po->po.getParentId().equals(cityVo.getId())).collect(Collectors.toList());
                List<AreaVO> areaVos=pos2vos(areaPos);
                cityVo.setChildren(areaVos);
            }
        }
        return provinceVos;
    }

    private List<AreaVO> pos2vos(List<AreaPO> pos) {
        if (CollectionUtils.isEmpty(pos)){
            return new ArrayList<>();
        }
        List<AreaVO> vos=pos.stream().map(po->{
            AreaVO vo=new AreaVO();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }
}
